/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlet1;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Thet Paing Soe
 */
@WebServlet(name = "ServletExample1", urlPatterns = {"/create-user"})
public class ServletExample1 extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    String btnsubmit=null;
    String btnsave=null;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ServletExample1</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ServletExample1 at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }
    @Override 
    protected void doPost(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException
    {
        btnsubmit=request.getParameter("btnsubmit");
        btnsave=request.getParameter("btnsave");
        if(btnsave!=null){
            Cookie Cname=new Cookie("Cname",request.getParameter("txtName"));
            response.addCookie(Cname);
            Cookie Cemail=new Cookie("Cemail",request.getParameter("txtemail"));
            response.addCookie(Cemail);
        }
        
        if(btnsubmit!=null){
        String name=request.getParameter("name");
        String countries=request.getParameter("countries");
        String email=request.getParameter("email");
        String letter=request.getParameter("receive_newsletter");
        
        String food=request.getParameter("food");
        String movies=request.getParameter("movies");
        String sports=request.getParameter("sports");
        String []gender=request.getParameterValues("gender");
        
        response.setStatus(HttpServletResponse.SC_CREATED);
        response.setContentType("text/html");
        
        PrintWriter out=response.getWriter();
        out.println("<htm><body>"+"Thank you for registering with us!"+name+
        "<br/>"+countries+"<br/>"+email+"<br/>");
        if(letter!=null)
        {
            out.println("Our monthly newsletter on ###,### will be send to your "+email+"<br/>");
        }
        out.println("</body></html>");
        }
    }
}
